What is @fastify/accepts?
@fastify/accepts is a Fastify plugin that provides a simple way to handle HTTP Accept headers. It allows you to easily determine the best content type, language, encoding, and charset to respond with based on the client's request.
What are @fastify/accepts's main functionalities?
Content Type Negotiation
This feature allows you to determine the best content type to respond with based on the client's Accept header. The code sample demonstrates how to respond with either JSON or HTML depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const type = accept.type(['json', 'html']);
if (type === 'json') {
reply.send({ message: 'Hello, JSON!' });
} else if (type === 'html') {
reply.type('text/html').send('<h1>Hello, HTML!</h1>');
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Language Negotiation
This feature allows you to determine the best language to respond with based on the client's Accept-Language header. The code sample demonstrates how to respond with either English or Spanish depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const lang = accept.language(['en', 'es']);
if (lang === 'en') {
reply.send({ message: 'Hello!' });
} else if (lang === 'es') {
reply.send({ message: '¡Hola!' });
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Encoding Negotiation
This feature allows you to determine the best encoding to respond with based on the client's Accept-Encoding header. The code sample demonstrates how to respond with either GZIP or Deflate encoding depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const encoding = accept.encoding(['gzip', 'deflate']);
if (encoding === 'gzip') {
reply.header('Content-Encoding', 'gzip').send('Hello, GZIP!');
} else if (encoding === 'deflate') {
reply.header('Content-Encoding', 'deflate').send('Hello, Deflate!');
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Charset Negotiation
This feature allows you to determine the best charset to respond with based on the client's Accept-Charset header. The code sample demonstrates how to respond with either UTF-8 or ISO-8859-1 charset depending on the client's preference.
const fastify = require('fastify')();
const accepts = require('@fastify/accepts');
fastify.register(accepts);
fastify.get('/', (request, reply) => {
const accept = request.accepts();
const charset = accept.charset(['utf-8', 'iso-8859-1']);
if (charset === 'utf-8') {
reply.header('Content-Type', 'text/plain; charset=utf-8').send('Hello, UTF-8!');
} else if (charset === 'iso-8859-1') {
reply.header('Content-Type', 'text/plain; charset=iso-8859-1').send('Hello, ISO-8859-1!');
} else {
reply.code(406).send('Not Acceptable');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Other packages similar to @fastify/accepts
accepts
The 'accepts' package is a content negotiation library for Node.js. It provides similar functionality to @fastify/accepts but is framework-agnostic, meaning it can be used with any Node.js server framework, not just Fastify.
negotiator
The 'negotiator' package is another content negotiation library for Node.js. It provides a more low-level API compared to 'accepts' and @fastify/accepts, giving you more control over the negotiation process but requiring more manual handling.
@fastify/accepts
![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)
Add an accepts parser to Fastify.
Install
npm i @fastify/accepts
Compatibility
Plugin version | Fastify version |
---|
^5.x | ^5.x |
^4.x | ^4.x |
^2.x | ^3.x |
^1.x | ^2.x |
^1.x | ^1.x |
Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin
in the table above.
See Fastify's LTS policy for more details.
Usage
const fastify = require('fastify')
const Boom = require('@hapi/boom')
fastify.register(require('@fastify/accepts'))
fastify.post('/', function (req, reply) {
const accept = req.accepts()
switch(accept.type(['json', 'html'])) {
case 'json':
reply.type('application/json').send({hello: 'world'})
break
case 'html':
reply.type('text/html').send('<b>hello, world!</b>')
break
default:
reply.send(Boom.notAcceptable('unacceptable'))
break
}
})
See accepts package for all available APIs.
This plugin adds to Request
object all Accepts
object methods.
fastify.post('/', function (req, reply) {
req.charset(['utf-8'])
req.charsets()
req.encoding(['gzip', 'compress'])
req.encodings()
req.language(['es', 'en'])
req.languages()
req.type(['image/png', 'image/tiff'])
req.types()
})
Options
decorateReply
If true
, the Reply
object will be decorated with the requestAccepts
, requestTypes
, requestCharsets
, requestEncodings
, and requestLanguages
methods, which will return the corresponding values from the Request
object. Default: false
.
License
Licensed under MIT.